import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/login',
    name: 'Login',
    component: ()=>import('../views/Login.vue')
  },
  {
    path: '/',
    name: 'Manage',
    component: () => import('../views/Manage.vue'),
    redirect: "/home",
    children:[
      { path: 'home', name: 'Home', component: () => import('../views/home/Home.vue') },
      { path: 'user', name: 'User', component: () => import('../views/user/Index.vue') },
      { path: 'productCategory', name: 'ProductCategory', component: () => import('../views/product/ProductCategory.vue') },
      { path: 'systemCategory', name: 'SystemCategory', component: () => import('../views/systemConfig/SystemCategory.vue') },
      { path: 'verificationCode', name: 'VerificationCode', component: () => import('../views/systemConfig/VerificationCode.vue') },
      { path: 'article', name: 'Article', component: () => import('../views/article/Article.vue') },
      { path: 'article/insertArticle', name: 'InsertArticle', component: () => import('../views/article/InsertArticle.vue') },
      { path: 'article/articleDetails', name: 'ArticleDetails', component: () => import('../views/article/ArticleDetails.vue') },
      { path: 'fileValue', name: 'FileValue', component: () => import('../views/systemConfig/FileValue.vue') },
      { path: 'product', name: 'Product', component: () => import('../views/product/Product.vue') },
      { path: 'product/productDetails', name: 'ProductDetails', component: () => import('../views/product/ProductDetails.vue') },
      { path: 'product/insertProduct', name: 'InsertProduct', component: () => import('../views/product/InsertProduct.vue') },
      { path: 'sysConfig', name: 'SysConfig', component: () => import('../views/systemConfig/SysConfig.vue') },
    ]
  },

]

const router = new VueRouter({
  mode: 'hash',
  routes
})

router.beforeEach((to, from, next) => {
  let isLogin = localStorage.getItem("user");
  let time_login = new Date(localStorage.getItem("loginTime")).valueOf()
  let time_now = new Date().valueOf()
  document.title = '云视源'
  if (to.path == '/login') return next();
  if (!isLogin||time_now>time_login) {
    return next('/login')
  }
  next()
})

export default router
